// pages/address/add.js
import WxValidate from '../../utils/WxValidate'
import { addUserAddr, getUserAddr } from '../../utils/apis'
import { alert, getCurrentAddress, reverseGeocoder, getPrevPage } from '../../utils/util'
Page({
  data: {},
  onLoad: function(options) {
    // 页面初始化 options为页面跳转所带来的参数
    this.id = options.id
    this.callback = options.callback || 'callback'
    this.initValidate()
    if (this.id) {
      this.loadData()
    } else {
      this.initAddress()
    }
  },
  initValidate() {
    this.validate = new WxValidate(
      {
        receiver: {
          required: true
        },
        phone: {
          required: true,
          tel: true
        },
        detail: {
          required: true
        }
      },
      {
        receiver: {
          required: '请输入您的姓名'
        },
        phone: {
          required: '请输入手机号',
          tel: '请输入有效手机号码'
        },
        detail: {
          required: '请输入门牌号等详细信息'
        }
      }
    )
  },
  initAddress() {
    var that = this
    getCurrentAddress(address => {
      that.setData({
        address
      })
    })
  },
  onChooseLocation(e) {
    var that = this
    that.setData({
      disabled: true
    })
    wx.chooseLocation({
      success: function(res) {
        var { name: title, address, longitude, latitude } = res
        var location = {
          longitude,
          latitude
        }
        reverseGeocoder({
          location,
          success(data) {
            that.setData({
              address: Object.assign(
                {
                  title,
                  address,
                  location
                },
                data
              ),
              disabled: false
            })
            console.log(that.data.address)
          },
          fail(err) {
            console.log(err)
          }
        })
      },
      fail(err) {
        getCurrentAddress(address => {
          that.setData({
            address,
            disabled: false
          })
        })
      }
    })
  },
  loadData() {
    var that = this
    var addr_id = this.id
    wx.showNavigationBarLoading()
    getUserAddr({
      addr_id
    })
      .then(data => {
        that.setData({
          receiver: data.nickname,
          phone: data.phone,
          detail: data.addressDetail,
          address: {
            title: data.areaName,
            location: {}
          }
        })
        wx.hideNavigationBarLoading()
      })
      .catch(err => {
        wx.hideNavigationBarLoading()
      })
  },
  formSubmit(e) {
    var that = this
    var { loading, address } = this.data
    if (loading) {
      return
    }

    if (!address) {
      return alert('请选择收货地址')
    }

    if (!this.validate.checkForm(e)) {
      const error = this.validate.errorList[0]
      return alert(error.msg)
    }
    this.setData({
      loading: true
    })
    var { receiver, phone, detail } = e.detail.value
    addUserAddr({
      id: this.id,
      type: 1,
      nickname: receiver,
      phone,
      addressDetail: detail,
      areaName: address.title,
      longitude: address.location.lng || address.location.longitude,
      latitude: address.location.lat || address.location.latitude
    })
      .then(result => {
        that.setData({
          loading: false
        })
        getPrevPage()[that.callback]()
        wx.navigateBack()
      })
      .catch(err => {
        that.setData({
          loading: false
        })
      })
  }
})
